﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.Core.Data;

namespace AG.Editor.ModelUI.Windows
{
    public partial class AGMoveFrameWindow : Form
    {
        AGModel _model;
        private AGAction _selAction;
        private AGDirection _selDirection;
        private List<AGFrame> _frameList;

        private AGAction _destAction;
        private AGDirection _destDirection;

        public AGMoveFrameWindow(AGModel model)
        {
            InitializeComponent();

            _frameList = new List<AGFrame>();
            _model = model;

            BindSrcModelStucture();
            BindModelStucture();
        }

        private void BindSrcModelStucture()
        {
            _ctlListSrcAction.Items.Clear();
            for (int index = 0; index < _model.Actions.Count; index++)
            {
                AGAction modelAction = _model.Actions[index];
                _ctlListSrcAction.Items.Add(modelAction);
            }
        }

        private void BindModelStucture()
        {
            _ctlListDestAction.Items.Clear();
            for (int index = 0; index < _model.Actions.Count; index++)
            {
                AGAction modelAction = _model.Actions[index];
                _ctlListDestAction.Items.Add(modelAction);
            }
        }

        private void _ctlBtnOK_Click(object sender, EventArgs e)
        {
            if (_selAction == _destAction && _selDirection == _destDirection)
            {
                return;
            }

            // 从src里移除frame
            for (int index = 0; index < _frameList.Count; index++)
            {
                _selDirection.RemoveFrame(_frameList[index].Id);
            }
            //
            for (int index = 0; index < _selDirection.Frames.Count; index++)
            {
                _selDirection.Frames[index].Id = index;
            }

            // frame添加到sel里
            int selMaxFrameId = 0;
            if (_destDirection.Frames.Count > 0)
            {
                selMaxFrameId = _destDirection.Frames.Last().Id;
                selMaxFrameId++;
            }
            for (int index = 0; index < _frameList.Count; index++)
            {
                _frameList[index].Id = selMaxFrameId;
                _destDirection.AddFrame(_frameList[index]);
                selMaxFrameId++;
            }

            _frameList.Clear();
            _ctlPanelFrame.Controls.Clear();

            BindSrcFrameList();
            BindDestFrameList();
            MessageBox.Show("转移完毕");
        }

        private void _ctlBtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void _ctlListSrcAction_SelectedIndexChanged(object sender, EventArgs e)
        {
            _selAction = _ctlListSrcAction.SelectedItem as AGAction;

            _selDirection = _ctlListSrcDirection.SelectedItem as AGDirection;
            int selDirectionId = -1;
            if (_selDirection != null)
            {
                selDirectionId = _selDirection.Id;
            }
            _ctlListSrcDirection.Items.Clear();
            for (int index = 0; index < _selAction.Directions.Count; index++)
            {
                AGDirection direction = _selAction.Directions[index];
                _ctlListSrcDirection.Items.Add(direction);
                if (selDirectionId != -1 && direction.Id == selDirectionId)
                {
                    _ctlListSrcDirection.SelectedIndex = index;
                }
            }
        }

        private void _ctlListSrcDirection_SelectedIndexChanged(object sender, EventArgs e)
        {
            _selDirection = _ctlListSrcDirection.SelectedItem as AGDirection;
            BindSrcFrameList();
        }

        private void BindSrcFrameList()
        {
            _ctlListSrcFrame.Items.Clear();
            _frameList = new List<AGFrame>();
            for (int index = 0; index < _selDirection.Frames.Count; index++)
            {
                _ctlListSrcFrame.Items.Add(_selDirection.Frames[index]);
            }
        }

        private void _ctlListSrcFrame_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                _frameList.Add(_ctlListSrcFrame.Items[e.Index] as AGFrame);
            }
            else
            {
                _frameList.Remove(_ctlListSrcFrame.Items[e.Index] as AGFrame);
            }

            _ctlPanelFrame.Controls.Clear();
            for (int index = 0; index < _frameList.Count; index++)
            {
                AGFrame frame = _frameList[index];

                string modelFolder = AG.Editor.Core.AGEContext.Current.EProject.GetFolder(frame.Direction.Action.Model);
                string frameFilePath = string.Format("{0}\\{1}", modelFolder, frame.ImageFileName);
                PictureBox picBox = new PictureBox();
                picBox.Image = new Bitmap(frameFilePath);
                picBox.SizeMode = PictureBoxSizeMode.Normal;
                picBox.Width = picBox.Image.Width;
                picBox.Height = picBox.Image.Height;

                _ctlPanelFrame.Controls.Add(picBox);
            }
        }

        private void _ctlListDestAction_SelectedIndexChanged(object sender, EventArgs e)
        {
            _destAction = this._ctlListDestAction.SelectedItem as AGAction;
            _destDirection = this._ctlListDestDirection.SelectedItem as AGDirection;

            int selDirectionId = -1;
            if (_destDirection != null)
            {
                selDirectionId = _destDirection.Id;
            }
            _ctlListDestDirection.Items.Clear();

            for (int index = 0; index < _destAction.Directions.Count; index++)
            {
                _ctlListDestDirection.Items.Add(_destAction.Directions[index]);
                if (selDirectionId!= -1 && _destAction.Directions[index].Id == selDirectionId)
                {
                    _ctlListDestDirection.SelectedIndex = index;
                }
            }
        }

        private void _ctlListDestDirection_SelectedIndexChanged(object sender, EventArgs e)
        {
            _destDirection = this._ctlListDestDirection.SelectedItem as AGDirection;
            BindDestFrameList();
        }

        private void BindDestFrameList()
        {
            _ctlListDestFrame.Items.Clear();

            _ctlPanelDestFrame.Controls.Clear();
            for (int index = 0; index < _destDirection.Frames.Count; index++)
            {
                _ctlListDestFrame.Items.Add(_destDirection.Frames[index]);

                AGFrame frame = _destDirection.Frames[index];

                string modelFolder = AG.Editor.Core.AGEContext.Current.EProject.GetFolder(frame.Direction.Action.Model);
                string frameFilePath = string.Format("{0}\\{1}", modelFolder, frame.ImageFileName);
                PictureBox picBox = new PictureBox();
                picBox.Image = new Bitmap(frameFilePath);
                picBox.SizeMode = PictureBoxSizeMode.Normal;
                picBox.Width = picBox.Image.Width;
                picBox.Height = picBox.Image.Height;

                _ctlPanelDestFrame.Controls.Add(picBox);
            }
        }
    }
}
